package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.DaoWithdrawRecord;
import com.sc.nft.entity.SandWithdrawRecord;
import com.sc.nft.entity.vo.MoneyRiskPageVO;
import com.sc.nft.enums.WalletChannelEnum;
import com.sc.nft.mapper.DaoWithdrawRecordMapper;
import com.sc.nft.mapper.SandWithdrawRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Slf4j
@Repository
public class DaoWithdrawRecordDao extends ServiceImpl<DaoWithdrawRecordMapper, DaoWithdrawRecord> {

    public void success(String orderNo){
        LambdaUpdateWrapper<DaoWithdrawRecord> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(DaoWithdrawRecord::getStatus,1);
        wrapper.eq(DaoWithdrawRecord::getCustomerOrderNo,orderNo);
        update(new DaoWithdrawRecord(),wrapper);
    }

    public void fail(String orderNo,String remark){
        LambdaUpdateWrapper<DaoWithdrawRecord> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(DaoWithdrawRecord::getStatus,2);
        wrapper.set(DaoWithdrawRecord::getRemark,remark);
        wrapper.eq(DaoWithdrawRecord::getCustomerOrderNo,orderNo);
        update(new DaoWithdrawRecord(),wrapper);
    }

    public Page<DaoWithdrawRecord> getByWalletId(Long walletId,Date startTime,Date endTime,Integer status,Page page){
        return lambdaQuery().eq(DaoWithdrawRecord::getWalletId,walletId)
                .ge(startTime!=null,DaoWithdrawRecord::getCreateTime,startTime)
                .le(endTime!=null,DaoWithdrawRecord::getCreateTime,endTime)
                .eq(status!=null,DaoWithdrawRecord::getStatus,status)
                .orderByDesc(DaoWithdrawRecord::getId).page(page);
    }
    public List<DaoWithdrawRecord> syncStatus() {
        LambdaQueryWrapper<DaoWithdrawRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DaoWithdrawRecord::getStatus,0);
        DateTime now = DateTime.now();
        wrapper.lt(DaoWithdrawRecord::getModifyTime,DateUtil.offsetMinute(now,-30));
        wrapper.last("limit 500");
        return list(wrapper);
    }
}